package car.aistar.controller;

import car.aistar.entity.Admin;
import car.aistar.entity.Seller;
import car.aistar.entity.User;
import car.aistar.entity.UserDetail;
import car.aistar.service.AdminService;
import car.aistar.service.SellerService;
import car.aistar.service.UserDetailService;
import car.aistar.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 本类用于：
 *
 * @author 许华钢
 * @Classname logAndRegistController
 * @Description TODO
 * @date 2020/10/3 19:00
 */
@Controller
@RequestMapping("/logAndRegist")
public class logAndRegistController extends HttpServlet {
    @Autowired
    private UserService userService;
    @Autowired
    private UserDetailService userDetailService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private AdminService adminService;

    /**
     * 许华钢
     *
     * @param model
     * @param username 用户账号
     * @param password 账号密码
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping("/log")
    public String view(Model model, String username, String password,String flag, HttpServletRequest req, HttpServletResponse resp) {

//        测试是否为用户
        User user = new User();
        user.setPhone(username);
//        System.out.println(user.getUId());

//        测试是否为销售员
        Seller seller = new Seller();
        seller.setAccount(username);

//        测试是否为管理
        Admin admin = new Admin();
        admin.setAdAccount(username);

//        检验输出flag
        String flagStr = String.valueOf(flag);

//        判断service是否找到数据，无数据其大小为0
        if (0!=userService.queryAll(user).size()){
            User userNameLog = userService.queryAll(user).get(0);
            if (password.equals(userNameLog.getPwd())){
                //清空session
                req.getSession().invalidate();

                // 设置session以便于跳转页面接受数据
                req.getSession().setAttribute("user",userNameLog);
                // 设置session以便于确认为用户
                req.getSession().setAttribute("roles",1);

                // session作用域存储用户名（用户名不存在时，存储账号），以便于导航栏给与提示
                if (!userNameLog.getUname().equalsIgnoreCase("null")){
                    // 存储用户名
                    req.getSession().setAttribute("name",userNameLog.getUname());
                    // 存储用户id
                    req.getSession().setAttribute("uid",userNameLog.getUId());
                }else {
                    // 存储账号
                    req.getSession().setAttribute("name",username);
                }


                if (!flagStr.equalsIgnoreCase("null")){
                    req.getSession().setAttribute("username",username);
                    req.getSession().setAttribute("password",password);
                    req.getSession().setAttribute("flag",true);
                }
                if (flagStr.equalsIgnoreCase("null")){
                    req.getSession().setAttribute("username","");
                    req.getSession().setAttribute("password","");
                    req.getSession().setAttribute("flag",null);
                }

//                跳转到首页
                return "/index/index02";

//                controller内部跳转
//                return "forward:/carDetail/buyCar/carInfo.action";
            }else {
                req.getSession().setAttribute("error",2);
                return "/logAndRegist/login";
            }
        }else if (0!=sellerService.queryAll(seller).size()){
            Seller sellerLog = sellerService.queryAll(seller).get(0);
//            获取销售人员id
            Long id = sellerLog.getSid();
            if (password.equals(sellerLog.getPwd())){
//                model.addAttribute(sellerLog);

                //清空session
                req.getSession().invalidate();

                // 设置session以便于跳转页面接受数据
                req.getSession().setAttribute("user",sellerLog);
                // 设置session以便于当前所在页面
                req.getSession().setAttribute("roles",2);

                // session作用域存储用户名（用户名不存在时，存储账号），以便于导航栏给与提示
                if (!sellerLog.getUname().equalsIgnoreCase("null")){
                    // 存储销售人员名
                    req.getSession().setAttribute("name",sellerLog.getUname());
                }else {
                    // 存储销售员账号
                    req.getSession().setAttribute("name",username);
                }

                if (!flagStr.equalsIgnoreCase("null")){
                    req.getSession().setAttribute("username",username);
                    req.getSession().setAttribute("password",password);
                    req.getSession().setAttribute("flag",true);
                }
                if (flagStr.equalsIgnoreCase("null")){
                    req.getSession().setAttribute("username","");
                    req.getSession().setAttribute("password","");
                    req.getSession().setAttribute("flag",null);
                }

//                跳转到销售员操作页面
//                req.getSession().setAttribute("id",id);
                req.getSession().setAttribute("selid",id);
                return "/adCenter/sales";
            }else {
                req.getSession().setAttribute("error",3);
                return "/logAndRegist/login";
            }
        }else if (0!=adminService.queryAll(admin).size()){
            Admin adminLog = adminService.queryAll(admin).get(0);
            if (password.equals(adminLog.getAdPassword())){
//                model.addAttribute(adminLog);

                //清空session
                req.getSession().invalidate();

                // 设置session以便于跳转页面接受数据
                req.getSession().setAttribute("user",adminLog);
                // 设置session以便于当前所在页面
                req.getSession().setAttribute("roles",3);

                // session作用域存储用户名（用户名不存在时，存储账号），以便于导航栏给与提示
                if (!adminLog.getAdName().equalsIgnoreCase("null")){
                    // 存储用户名
                    req.getSession().setAttribute("name",adminLog.getAdName());
                }else {
                    // 存储账号
                    req.getSession().setAttribute("name",username);
                }

                if (!flagStr.equalsIgnoreCase("null")){
                    req.getSession().setAttribute("username",username);
                    req.getSession().setAttribute("password",password);
                    req.getSession().setAttribute("flag",true);
                }
                if (flagStr.equalsIgnoreCase("null")){
                    req.getSession().setAttribute("username","");
                    req.getSession().setAttribute("password","");
                    req.getSession().setAttribute("flag",null);
                }

//                跳转到管理员操作页面
                return "/adCenter/admin";
//                return "forward:/carDetail/buyCar/carInfo.action";
            }else {
                req.getSession().setAttribute("error",4);
                return "/logAndRegist/login";
            }
        }else {
            req.getSession().setAttribute("error",1);
            return "/logAndRegist/login";
        }
    }

    /**
     * 本类用于：
     *
     * @author 许华钢
     * @Description TODO
     * @date 2020/10/3 19:00
     */
    @RequestMapping("/register")
    public String reg(Model model,String username,String phone,String pwd,HttpServletRequest req, HttpServletResponse resp){

        User userReg = new User();
        userReg.setPhone(phone);


        if (0==userService.queryAll(userReg).size()){
            userReg.setPwd(pwd);
            userReg.setUname(username);
            // 向用户登录表中插入数据
            userService.insert(userReg);
            // 创建用户详情实例，以便于赋值
            UserDetail userDetailInsert = new UserDetail();
            userDetailInsert.setUphone(phone);
            userDetailInsert.setUpwd(pwd);
            userDetailInsert.setUname(username);
            userDetailInsert.setUId(userReg.getUId());
            // 向用户详情表中插入数据
            userDetailService.insert(userDetailInsert);
            // 跳转到登录页面
            return "/logAndRegist/login";
        }else {
            req.getSession().setAttribute("regError",1);
            return "/logAndRegist/register";
        }
    }

    /**
     * 本类用于：
     *
     * @author 许华钢
     * @Description TODO
     * @date 2020/10/3 19:00
     */
    @RequestMapping("/exit")
    public String exit(HttpServletRequest req, HttpServletResponse resp){
        //清空session
        req.getSession().invalidate();
        return "/logAndRegist/login";
    }

}
